//
//  TeacherCourseViewController.m
//  jinnengExam
//
//  Created by mac on 2020/8/11.
//  Copyright © 2020 jznrj. All rights reserved.
//

#import "TeacherCourseViewController.h"
#import "CourseCodeViewController.h"
#import "ClassSignViewController.h"
#import "CourseAppriseViewController.h"
#import "ClassCourseDetailViewController.h"

@interface TeacherCourseViewController ()
@property(nonatomic,strong) JznWebView *jznWebView;
@property(nonatomic) int page;
@end

@implementation TeacherCourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程列表";
    // Do any additional setup after loading the view.
    UINavigationBar * bar = self.navigationController.navigationBar;
       UIImage *bgImage = [ColorUtil navigationBarColor:[ColorUtil titleColor] BarSize:bar.frame alphe:1.0];
       [bar setBackgroundImage:bgImage forBarMetrics:UIBarMetricsDefault];
       [bar setTintColor:[UIColor whiteColor]];
       [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]}forState:UIControlStateNormal];
       [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    [self initView];
}

-(void) viewDidAppear:(BOOL)animated{
    
}

-(void) initView{
   [self.view addSubview:self.jznWebView];
    self.jznWebView.scrollView.bounces = NO;
   [self.jznWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.bottom.equalTo(self.view);
        }
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.jznWebView loadLocalFile:@"/ios_jinneng/teacherCourse" type:@"html"];
    
    [self.jznWebView.scrollView.mj_header setBackgroundColor:[UIColor whiteColor]];
    self.jznWebView.scrollView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self requestLearn:self.page];
        
    }];
    
    self.jznWebView.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestLearn:++self.page];
    }];
    
    [self registerJSFunction];
}

-(void) registerJSFunction{
    
    
    [self.webViewBridge registerHandler:@"toCourseDetail" handler:^(id data, WVJBResponseCallback responseCallback) {
       // NSString *trainingInfo =  [data mj_JSONString];
        NSLog(@"trainingInfo:%@",data);
        dispatch_async(dispatch_get_main_queue(), ^(){
            ClassCourseDetailViewController *vc = [[ClassCourseDetailViewController alloc]init];
              vc.trainingInfo = data;
              [self.navigationController pushViewController:vc animated:YES];
        });
    }];
    
    [self.webViewBridge registerHandler:@"toClassCode" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSString *courseId = data[@"courseId"];
        NSString *courseName = data[@"courseName"];
        dispatch_async(dispatch_get_main_queue(), ^(){
              CourseCodeViewController *vc = [[CourseCodeViewController alloc]init];
              [vc setTrainingClassId:courseId];
              [vc setSignType:@"0"];
              [vc setTrainingClassName:courseName];
            [self.navigationController pushViewController:vc animated:YES];
        });
    }];
    
    [self.webViewBridge registerHandler:@"toClassSign" handler:^(id data, WVJBResponseCallback responseCallback) {
         NSString *courseId = data[@"courseId"];
         dispatch_async(dispatch_get_main_queue(), ^(){
               ClassSignViewController *vc = [[ClassSignViewController alloc]init];
               [vc setClassCourseId:courseId];
             [self.navigationController pushViewController:vc animated:YES];
         });
     }];
    
    [self.webViewBridge registerHandler:@"toApprise" handler:^(id data, WVJBResponseCallback responseCallback) {
         NSString *courseId = data[@"courseId"];
         dispatch_async(dispatch_get_main_queue(), ^(){
               CourseAppriseViewController *vc = [[CourseAppriseViewController alloc]init];
               [vc setClassCourseId:courseId];
             [self.navigationController pushViewController:vc animated:YES];
         });
     }];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    [self requestLearn:1];
}

-(void) requestLearn:(int) page{
    NSString *url = [NSString stringWithFormat:@"%@/classCourse/findAppClassCourse" ,[URLUtil hostName]];
    NSDictionary *params = @{@"professionalClassId":self.professionalClassId};
    NSLog(@"getCourseList:%@",self.professionalClassId);
    [JZNRequestUtil requestUrl:url requestParams:params success:^(DataResult *dataResult) {
        if(dataResult.code == 0){
            if (page == 1) {
               NSString *clear = @"clear()";
                [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                    
                }];
               [self.jznWebView.scrollView.mj_header endRefreshing];
               [self.jznWebView.scrollView.mj_footer resetNoMoreData];
           }
            
            NSString *clear = [NSString stringWithFormat:@"getCourseList(%@)",[dataResult.data mj_JSONString]];
           [self.jznWebView evaluateJavaScript:clear completionHandler:^(id _Nullable data, NSError * _Nullable error) {
                
            }];

        }else {
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
